/**
 *
 * @param {object} response
 * @param {string} url
 * @param {boolean} type
 * @param {Function} callback
 */
function redirect(response, url,type,callback) {
  new Promise((resolve,reject)=>{
    response ? resolve() : reject();
  }).then(
      ()=>{
        new Promise((resolve,reject)=>{
          type ? resolve() : reject();
        }).then(
            ()=>{
              response.writeHead(301, {'Location': url});
              response.end();
              callback(null,301);
            }
            ,()=>{
              response.writeHead(302, {'Location': url});
              response.end();
              callback(null,302);
            }
        ).catch((error)=>{
          arguments[arguments.length-1](error,null);
        })
      }
      ,()=>{
        arguments[arguments.length-1](new SyntaxError("response can't be undefined"),null);
      }
  ).catch((error) => {
    arguments[arguments.length-1](error, null);
  })
}

module.exports=redirect

